package com.domi.controller;

import com.domi.bean.RestfulResponse;
import com.domi.bean.RestfulResponse.ResponseCode;
import com.domi.bean.User;
import org.springframework.web.bind.annotation.CookieValue;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("/user")
public class UserController {

    @GetMapping("/")
    public RestfulResponse index() {
        return RestfulResponse.create(ResponseCode.SUCCESS, "用户管理中心服务, 接口信息见user-center/api.http文件");
    }

    @PostMapping("/login")
    public RestfulResponse login(String username, String password) {
        Optional<User> userOptional = userListCache.stream()
                .filter(user -> user.getUsername().equals(username) && user.getPassword().equals(password))
                .findFirst();
        return userOptional.map(user -> RestfulResponse.create(ResponseCode.SUCCESS, user))
                .orElse(RestfulResponse.create(ResponseCode.FAIL));
    }

    @GetMapping("/info")
    public RestfulResponse info(@CookieValue(name = "username", defaultValue = "") String username) {
        Optional<User> userOptional = userListCache.stream()
                .filter(user -> user.getUsername().equals(username))
                .findFirst();
        return userOptional.map(user -> RestfulResponse.create(ResponseCode.SUCCESS, user))
                .orElse(RestfulResponse.create(ResponseCode.FAIL));
    }

    @GetMapping("/info/{id}")
    public RestfulResponse information(@PathVariable Integer id) {
        Optional<User> userOptional = userListCache.stream()
                .filter(user -> user.getId().equals(id))
                .findFirst();
        return userOptional.map(user -> RestfulResponse.create(ResponseCode.SUCCESS, user))
                .orElse(RestfulResponse.create(ResponseCode.FAIL));
    }

    @GetMapping("/detail")
    public RestfulResponse detail(@RequestHeader(name = "X-Request-Id", defaultValue = "") String requestId) {
        Optional<User> userOptional = userListCache.stream()
                .filter(user -> user.getId().toString().equals(requestId))
                .findFirst();
        return userOptional.map(user -> RestfulResponse.create(ResponseCode.SUCCESS, user))
                .orElse(RestfulResponse.create(ResponseCode.FAIL));
    }

    @PostMapping("/register")
    public RestfulResponse register(@RequestBody User user) {
        Optional<Integer> maxIdOptional = userListCache.stream()
                .map(User::getId)
                .max(Integer::compareTo);
        Integer newId = maxIdOptional.map(id -> ++id).orElse(1);
        user.setId(newId);
        // add(user);
        return RestfulResponse.create(ResponseCode.SUCCESS, newId);
    }

    @GetMapping("/flight")
    public RestfulResponse flight() throws InterruptedException {
        int seed = ThreadLocalRandom.current().nextInt(2, 6);
        System.err.println("========= sleep " + seed + " second =========");
        TimeUnit.SECONDS.sleep(seed);
        return RestfulResponse.create(ResponseCode.SUCCESS, "I woke up");
    }

    private static final List<User> userListCache = Arrays.asList(
            new User(3314, "admin", "admin", "1203130383", "笼秋月色", "13800138000",
                    Date.from(LocalDateTime.parse("2022-09-20T09:22:18.276").atZone(ZoneId.systemDefault()).toInstant())),
            new User(3315, "guest", "guest", "1525336367", "滴滴的车", "15800159000",
                    Date.from(LocalDateTime.parse("2022-09-21T20:34:45.443").atZone(ZoneId.systemDefault()).toInstant()))
    );


}